//
//  CRBaseSetController.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRBaseSetController.h"
#import "CRSetGroup.h"
#import "CRSetItem.h"
#import "CRSetArrowItem.h"
#import "CRSetCell.h"

@interface CRBaseSetController ()

@end

@implementation CRBaseSetController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
}


- (NSMutableArray *)data
{
    if (!_data) {
        _data = [NSMutableArray array];
 
    }
    return _data;
}



- (instancetype)init
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        
    }
    return self;
}



#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.data.count;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    CRSetGroup *subdata = self.data[section];
    return subdata.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    CRSetCell *cell = [CRSetCell cellWithTableView:tableView];
    
    // 2. 设置cell的数据
    CRSetGroup *group = self.data[indexPath.section];
    CRSetItem *item = group.items[indexPath.row];
    
    cell.item = item;
    
    return cell;
}


// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 取消选中这行
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    CRSetGroup *group = self.data[indexPath.section];
    CRSetItem *item = group.items[indexPath.row];
    
    if (item.option) {  // block有值，(点击这个cell 有特定的操作需要执行)；
        item.option();
    }
    
    if ([item isKindOfClass:[CRSetArrowItem class]]) {  // 箭头
        CRSetArrowItem *arrowItem = (CRSetArrowItem *)item;
        // 如果没有需要跳转的控制器
        if (arrowItem.desvcClass == nil) return;
        
        UIViewController *vc = [[arrowItem.desvcClass alloc] init];
        vc.title = item.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
}



// 头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CRSetGroup *group = self.data[section];
    
    return group.header;
}

// 尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    CRSetGroup *group = self.data[section];
    return group.footer;
    
}



@end








